package com.gladlee.time;


import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.SimpleChannelHandler;

public class TimeServerHandler extends SimpleChannelHandler {

	@Override
	public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
			throws Exception {
		// TODO Auto-generated method stub
//		super.channelOpen(ctx, e);
//		TimeServer.
	}

	@Override
	public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
			throws Exception {
		// TODO Auto-generated method stub
		Channel ch=e.getChannel();
		ChannelBuffer time=ChannelBuffers.buffer(4);
		time.writeInt((int)(System.currentTimeMillis()/1000L+2208988800L));
		
		ChannelFuture f=ch.write(time);
		f.addListener(new ChannelFutureListener(){
			public void operationComplete(ChannelFuture future){
				Channel ch=future.getChannel();
				ch.close();
			}
		});
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
			throws Exception {
		// TODO Auto-generated method stub
		e.getCause().printStackTrace();
		e.getChannel().close();
	}

}
